﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace 使用及对比
{
    class Program
    {
        static void Main(string[] args)
        {

            new 双核心处理().Test();
            Console.Read();






            Console.WriteLine("请输入是否设置最小线程池创建个数1设置，其它不设置");
            var sc = Console.ReadKey();
            if (sc.Key== ConsoleKey.D1)
            {
                //ThreadPool.SetMaxThreads(1000, 1000);
                //设置最小线程数显注改善启动速度
                ThreadPool.SetMinThreads(200, 1000);
                Console.WriteLine("ThreadPool.SetMinThreads(200, 1000);设置");

            }

            var b = new 性能记数器();

            //获取本程序的pid
            var process = System.Diagnostics.Process.GetCurrentProcess();

            var ins = b.GetProcessInstanceName(process.Id);
            b.获取所有性能记数器的名称和值列表("Process", ins);

            Console.WriteLine("--------------------------------------------------------");
            b.PrintProcessInfo(ins);
            var stack = System.Threading.CompressedStack.GetCompressedStack();

            //获取线程TID
            var id = System.Threading.Thread.CurrentThread.ManagedThreadId;
            var threads = System.Diagnostics.Process.GetCurrentProcess().Threads.Cast<System.Diagnostics.ProcessThread>();
            var ths = threads.Where(p => p.Id == id).FirstOrDefault();



            Console.WriteLine("--------------------------输出所有的记数器选项------------------------------");
            var list = System.Diagnostics.PerformanceCounterCategory.GetCategories().ToList();
            var sblist = list.Select(p => p.CategoryName);
            list.ForEach(o =>
            {
                string help = "";

                try { help = o.CategoryHelp; }
                catch { }

                Console.WriteLine("名称:{0},{1},{2}", o.CategoryName, o.CategoryType,help);
            });

            //线程信息打印();


            //模拟数据
            //new 接入_解析_处理_入库四步骤测试().Run();



            //测试BlockIng是否可以控制流量();

            Test多层级Blocking();


            while (true)
            {
                Console.Read();
            }
        }

      static  System.Collections.Concurrent.BlockingCollection<string> abc =
            new System.Collections.Concurrent.BlockingCollection<string>(10);

        static void 测试BlockIng是否可以控制流量()
        {

            //添加总数
            long inn = 0;
            double sp = 0;
            long per = 0;

            System.Threading.Tasks.Task.Run(() => {
                while (true)
                {


                    foreach (var item in abc.GetType().GetProperties())
                    {
                        Console.WriteLine("{0}:{1}", item.Name, item.GetValue(abc));
                    }

                    long cc = inn - per;
                    sp = cc / 5.0;
                    per = inn;
                    Console.WriteLine(" 速度{0:F2}/s", sp);
                    System.Threading.Thread.Sleep(5000);
                }
            });

            System.Random dom = new Random();
            System.Threading.Tasks.Task.Run(() => {
               string xx=  dom.Next().ToString();
                while (true)
                {
                    abc.Add(xx);
                    System.Threading.Interlocked.Increment(ref inn);
                }
            });


            System.Threading.Tasks.Task.Run(() => {
                while (true)
                {
                    abc.Take();
                }
            });


        }



        static void Test多层级Blocking()
        {
            List<QueueBlocking<byte[]>> abc = new List<QueueBlocking<byte[]>>();
            for (int i = 0; i < 100; i++)
            {
                var aa = new QueueBlocking<byte[]>();
                aa.Quue.Name = "Q"+i;
                abc.Add(aa);

            }

            //生成树结构
            var r = abc.Aggregate((a1, c) =>
            {
                a1.ChildrenQueue = c;
                return c;
            });

            abc[0].Start();

            System.Threading.Tasks.Task task = new Task(() =>
            {
                byte[] add = new byte[1024 * 8];
                while (true)
                {
                    abc[0].Quue.Add(add);
                }
            });
            task.Start();

            System.Threading.Tasks.Task.Factory.StartNew(() =>
            {
                while (true)
                {
                    Console.WriteLine(
                        string.Join(";\r\n", (from px in abc
                                              let sp1 = px.Quue.CreSp
                                              let sp2 = px.Quue.PopSp
                                              let p=px.Quue
                                              select new
                                              {
                                                  Name = p.Name,
                                                  n1 = sp1.per,
                                                  sp = sp1.sp,
                                                  n2 = sp2.per,
                                                  sp2 = sp2.sp,
                                                  ss = p.queue.Count
                                              }
                                             ).Select(p => p.Name + "包含：" + p.ss + " " +
                        "生产：" + p.n1 + "^" + p.sp.ToString("F2") +
                        "/s,消费： " + p.n2 + "^" + p.sp2.ToString("F2") + "/s"
                        ))
                        );

                    System.Threading.Thread.Sleep(5000);

                }
            });
        }

        /// <summary>
        /// 
        /// </summary>
        public static void 线程信息打印()
        {
            System.Threading.Tasks.Task task = new Task(() =>
            {
                while (true)
                {
                    //输出线程
                    var threads = System.Diagnostics.Process.GetCurrentProcess().Threads.Cast<System.Diagnostics.ProcessThread>();
                    foreach (var thr in threads)
                    {
                        Console.WriteLine("线程ID:{0},启动时间:{1},用户时间:{2},总时间:{3}", thr.Id, thr.StartTime,thr.UserProcessorTime,thr.TotalProcessorTime);
                    }
                    System.Threading.Thread.Sleep(20 * 1000);
                }
            });
            task.Start();

        }
    }


}
